<template>
    <view class="imag-list">

     
            <view class="imag-box"  v-if="path" >
                 <image :src="path" class="bg" mode="aspectFill" />
                
               
            </view>
             <view class="imag-box" @tap="addImag">
               
                <image src="/static/images/upmedia_bg.png"  class="bg"/>
               
            </view>
       

       

     


    </view>
</template>
<script>

import {postUpLoadFile} from '@/request/actions'


export default {
   
        props:{
       
            idx:{
                type:Number,
                default:0
            }
        },
        data(){
            return{
                 path:''
            }
           
          
        },
        methods:{

            toPicList(i){
                this.$emit('setPicList',{
                    list:this.list,
                    idx:i
                })
            },
          
                addImag(){

                  
                      

                    uni.chooseImage({
                        count:1,
                        sizeType:'compressed',
                        success: res => {
                                let imags = []
                                if(res.errMsg == "chooseImage:ok"){
                                    
                                   let path = res.tempFilePaths[0];

                                  
                            

                                            postUpLoadFile(path).then(r=>{
                                           
                                                    let {data:res} = r;
                                                    res = JSON.parse(res);
                                                    
                                                    if(res.status == 200){
                                                        this.path = res.data;
                                                    
                                                        this.$emit('setImages',{
                                                            path:this.path,
                                                            idx:this.idx
                                                        });
                                                        
                                                  
                                                  }
                                                    
                                                    
                                                    
                                                    
                                                  
                                                   
                                                })

                               
                            



                          }
                              
                                   
                               
                        


                            }
                        })

             
                },
                del(i){
                    let arr = this.list;
                    arr.splice(i,1);
                    this.list = arr;
                    this.$emit('setImages',this.list);
                    this.$emit('setDelImageIdx',i);
                  
                   
                }
        },
        watch:{
            images(n,o){
            
                if(n.length){
                    this.list = [];
                    n.forEach(item=>{
                        this.list.push(item.imagesPath)
                    })
                  
             
                }
            }
        }
      
}
</script>
<style lang="scss">
    .imag-box{
        width: 145rpx;
        height: 145rpx;
        display: inline-block;
        vertical-align: top;
        margin-right: 20rpx;
        image{
             width: 145rpx;
             height: 145rpx;
        }
    }
</style>
